package cn.rui.service.impl;

import cn.rui.mapper.OrderSettingMapper;
import cn.rui.pojo.OrderSetting;
import cn.rui.service.OrderSettingService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 徽州大都督
 * @date 2020/8/26
 */
@Service //暴露服务
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    //将文件数据保存到数据库
    public void add(List<OrderSetting> data) {


        if (!CollectionUtils.isEmpty (data) && data.size () > 0) {

            //判断今天是否已经上传过,上传过就更新
            for (OrderSetting orderSetting : data) {
                long count = orderSettingMapper.findCountByOrderDate (orderSetting.getOrderDate ());
                if (count > 0) {
                    //已经进行了预约设置，执行更新操作
                    orderSettingMapper.updateNumberByOrderDate (orderSetting);
                } else {
                    //没有进行预约设置，执行新增操作
                    orderSettingMapper.add (orderSetting);
                }
            }
        }
    }

    @Override
    //根据月份查询对应的预约信息
    public List<Map<String, Object>> getOrderSettingByMonth(String date) { //格式: yyyy-MM
        String start = date + "-1";  //2020-08-1
        String end = date + "-31"; //2020-08-31
        Map<String, String> map = new HashMap<> ();
        map.put ("start", start);
        map.put ("end", end);
        List<OrderSetting> orderSettingList = orderSettingMapper.getOrderSettingByMonth (map);

        List<Map<String, Object>> maps = new ArrayList<> ();
        if (!CollectionUtils.isEmpty (orderSettingList) && orderSettingList.size () > 0) {
            for (OrderSetting setting : orderSettingList) {
                Map<String, Object> objectMap = new HashMap<> ();
                objectMap.put ("date", setting.getOrderDate ().getDate ());  //几号
                objectMap.put ("number", setting.getNumber ());
                objectMap.put ("reservations", setting.getReservations ());
                maps.add (objectMap);
            }
        }
        return maps;
    }

    @Override
    //修改可预约人数
    public void updateNumberByDate(String date,Integer number) {

        //判断当前日期是否已经进行了预约设置


        OrderSetting  orderSetting = orderSettingMapper.findByOrder (date);

        //System.out.println (orderSetting);
        if (!ObjectUtils.isEmpty (orderSetting)){
            //已经设置，执行更新操作
            orderSetting.setNumber (number);
            orderSettingMapper.updateNumberByOrderDate (orderSetting);
        }else {
            //没有设置，执行添加操作
            OrderSetting orderSetting1 = new OrderSetting ();
            orderSetting1.setNumber (number);
            SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
            Date parse = null;
            try {
                parse = sdf.parse (date);
            } catch (ParseException e) {
                e.printStackTrace ();
            }
            //System.out.println (parse);
            orderSetting1.setOrderDate (parse);
            orderSettingMapper.add (orderSetting1);
        }
    }
}
